/*
 * -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
 *
 *      Filename : progbar.cc
 *   Description : 
 *
 *       Version : 1.0
 *         CTime : Wed Nov 30 11:03:43 CST 2016
 *      Compiler : g++
 *        Author : Gallen Calade
 *        E-mail : 932441619@qq.com
 *
 * -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
 */

#include <cstdio>

#include "progbar.h"

/**
 *  Description:
 *      initialize progress 
 *  Parameters:
 *      
 *  Returns:
 *      
 *  Throws:
 *      
 *  Notes:
 *      
 */
template <typename T>
void CProgBar<T>::init()
{
    multiPower = (double)range / PROG_MAX_LEN;
    if(prog == nullptr)
    {
        prog = new char[PROG_MAX_LEN + 1];
    }

    if(style == PROG_BGC_STYLE)
    {
        memset(prog, 0x00, PROG_MAX_LEN + 1);
    }
    else
    {
        memset(prog, 32, PROG_MAX_LEN);
        memset(prog + PROG_MAX_LEN, 0x00, 1);
    }

}

/**
 *  Description:
 *      show progress position 
 *  Parameters:
 *      position
 *  Returns:
 *      
 *  Throws:
 *      
 *  Notes:
 *      
 */
template <typename T>
void CProgBar<T>::setpos(T pos)
{
    if(pos > range)
    {
        printf("ERROR: Out of progress bar range\n");
        return ;
    }
    int num = (int)(pos / multiPower);
    switch(style)
    {
        case PROG_NUM_STYLE:
            printf("\033[K\033[?25l\033[31m\033[1m%s%d%%\033[?25h\033[0m%s\r", title.c_str(), (int)(num * offset), note.c_str());
            fflush(stdout);
            break;
        case PROG_CHR_STYLE:
            memset(prog, chr, num);
            printf("\033[K\033[?25l\033[31m\033[1m%s[%-s]\033[32m%d%%\033[?25h\033[0m\033[%dG%s\r", title.c_str(), prog, (int)(num * offset), PROG_MAX_LEN + (int)title.length() + 7, note.c_str());
            fflush(stdout);
            break;
        case PROG_BGC_STYLE:
            memset(prog, 32, num);
            printf("\033[K\033[?25l\033[31m\033[1m%s\033[41m%s\033[32m%d%%\033[?25h\033[0m\033[%dG%s\r", title.c_str(), prog, (int)(num * offset), PROG_MAX_LEN + (int)title.length() + 5, note.c_str());
            fflush(stdout);
            break;
        default:
            return ;
    }
}
